package main

import (
	//"errors"
	"fmt"
)

type stack struct {
	size int // 栈容量
	top  int // 栈顶
	data []interface{}  // 数据
}

const stackSize = 15
var stackData = make([]interface{}, stackSize)

// 初始化栈
func initStack() stack {
	s := stack{
		size: stackSize,
		top: -1,
		data: stackData,
	}
	return s
}

func (this *stack) Empty() bool {
	// 判断栈是否为空
	return this.top == -1
}

func (this *stack) IsFull() bool {
	// 判断栈是否已满
	return this.top == this.size - 1
}

func (this *stack) Push(val interface{}) bool {
	// 入栈
	if this.IsFull() {
		fmt.Println("栈已满")
		//return errors.New("Stack isFull")
		return false
	}
	this.top += 1 // 栈顶加1
	//fmt.Println(val.(string))
	this.data[this.top] = val  // 将当前元素放在栈顶
	return true
}

func (this *stack) Pop() interface{}{
	if this.Empty(){
		fmt.Println("栈是空的")
		return nil
	}
	popIndex := this.top
	this.top--
	return this.data[popIndex]
}

func (this *stack) Travel() {
	if this.Empty(){
		fmt.Println("栈为空")
		return
	}
	for i:=0;i<=this.top;i++{
		fmt.Printf("%v  ", this.data[i])
	}
}

func (this *stack) Length() int {
	return this.top + 1
}

func (this *stack) Clear() {
	this.top = -1
}

func main() {
	s := initStack()
	s.Empty()
	fmt.Println(s.Push("hi"))
	fmt.Println(s.Push(20))
	fmt.Println(s.Push(100))
	fmt.Println(s.Push("go"))
	res := s.Pop()
	fmt.Println(res)
	s.Travel()
	fmt.Println()
	fmt.Println(s.Length())
	s.Clear()
	fmt.Println(s.Length())
}